package com.lixin.smartclassroom.ui.home.tab2.activity

import android.text.TextUtils
import android.widget.RadioGroup
import androidx.core.view.isVisible
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeDialog
import com.drake.net.utils.scopeNetLife
import com.drake.tooltip.dialog.BubbleDialog
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.addcgtimu
import com.lixin.smartclassroom.api.addpingjia
import com.lixin.smartclassroom.api.gettimu
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.bean.ImageUploadEvent
import com.lixin.smartclassroom.bean.NormalQuestionDto
import com.lixin.smartclassroom.common.recyclerDialog
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivitySetQuestionBinding
import com.lixin.smartclassroom.ui.home.adapter.answer
import com.lixin.smartclassroom.ui.home.adapter.selectImages
import com.lixin.smartclassroom.utils.UploadUtils
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import com.luck.picture.lib.entity.LocalMedia
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File
import java.util.Locale

/**
 * Description
 * Author: lwc
 * Date:2023/12/8 16:16
 */
class SetQuestionAct : BaseActivity<ActivitySetQuestionBinding>() {
    override fun getViewBinding() = ActivitySetQuestionBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<NormalQuestionDto>
    override fun title() = "设置题目"
    private var questionType = ""
    private var smallid = ""
    private var type = "1"//1单选  2多选  3主观题
    private var answerNumber = 0//答案数目
    private var ifright = "2"//是否有正确答案
    private var rightbiaoshi = ""//正确答案标识
    private var questionScore = 0
    private var weiduId = ""
    override fun textMenuContent() = "完成"

    override fun onTextMenuListener() {
        super.onTextMenuListener()
        addQuestionData(1)
    }

    override fun iniView() {
        initSelectImgView()

        questionType = getIntentString("questionType")
        smallid = getIntentString("smallid")

        getTimuData()

        listBean = ArrayList()
        binding.apply {

            when (questionType) {
                "2" -> {
                    llQuestionType.gone()
                    llEvaluationQuestions.visible()
                    llChoice.gone()
                    llChoiceAnswer.gone()
                }
            }


            rgState.setOnCheckedChangeListener { _, checkedId ->
                when (checkedId) {
                    R.id.rbSingleChoice -> {
                        type = "1"
                        llChoice.visible()
                        llChoiceAnswer.visible()
                        setAnswerNumberView()
                    }

                    R.id.rbChoices -> {
                        type = "2"
                        llChoice.visible()
                        llChoiceAnswer.visible()
                        setAnswerNumberView()
                    }

                    R.id.rbSubjective -> {
                        type = "3"
                        llChoice.gone()
                        llChoiceAnswer.gone()
                    }
                }
            }
        }

        receiveEvent<DataListBean>("weidu") {
            weiduId = it.id.toString()
        }

        receiveEvent<ImageUploadEvent>("tag_set_answer_pic_A") {
            imgUrlA = it.url
        }
        receiveEvent<ImageUploadEvent>("tag_set_answer_pic_B") {
            imgUrlB = it.url
        }
        receiveEvent<ImageUploadEvent>("tag_set_answer_pic_C") {
            imgUrlC = it.url
        }
        receiveEvent<ImageUploadEvent>("tag_set_answer_pic_D") {
            imgUrlD = it.url
        }
        receiveEvent<ImageUploadEvent>("tag_set_answer_pic_E") {
            imgUrlE = it.url
        }
    }

    private var localMediaListA = java.util.ArrayList<LocalMedia>()
    private var localMediaListB = java.util.ArrayList<LocalMedia>()
    private var localMediaListC = java.util.ArrayList<LocalMedia>()
    private var localMediaListD = java.util.ArrayList<LocalMedia>()
    private var localMediaListE = java.util.ArrayList<LocalMedia>()
    private var imgUrlA = ""
    private var imgUrlB = ""
    private var imgUrlC = ""
    private var imgUrlD = ""
    private var imgUrlE = ""
    private fun initSelectImgView() {
        binding.apply {
            selectImgA.selectImages(
                localMediaListA,
                this@SetQuestionAct,
                0,
                1,
                1,
                true,
                onClick = { }) { it ->
                localMediaListA.clear()
                localMediaListA.addAll(it)
                UploadUtils.uploadImages(localMediaListA, 0, "tag_set_answer_pic_A")
            }

            selectImgB.selectImages(
                localMediaListB,
                this@SetQuestionAct,
                0,
                1,
                1,
                true,
                onClick = { }) { it ->
                localMediaListB.clear()
                localMediaListB.addAll(it)
                UploadUtils.uploadImages(localMediaListB, 0, "tag_set_answer_pic_B")
            }

            selectImgC.selectImages(
                localMediaListC,
                this@SetQuestionAct,
                0,
                1,
                1,
                true,
                onClick = { }) { it ->
                localMediaListC.clear()
                localMediaListC.addAll(it)
                UploadUtils.uploadImages(localMediaListC, 0, "tag_set_answer_pic_C")
            }

            selectImgD.selectImages(
                localMediaListD,
                this@SetQuestionAct,
                0,
                1,
                1,
                true,
                onClick = { }) { it ->
                localMediaListD.clear()
                localMediaListD.addAll(it)
                UploadUtils.uploadImages(localMediaListD, 0, "tag_set_answer_pic_D")
            }

            selectImgE.selectImages(
                localMediaListE,
                this@SetQuestionAct,
                0,
                1,
                1,
                true,
                onClick = { }) { it ->
                localMediaListE.clear()
                localMediaListE.addAll(it)
                UploadUtils.uploadImages(localMediaListE, 0, "tag_set_answer_pic_E")
            }
        }
    }


    private fun getTimuData() {
        scopeDialog {
            val listBean = gettimu(smallid)

            binding.recycler.answer().apply {
                R.id.tvQuestionNo.onClick {

                }
            }.models = listBean
        }
    }

    private fun setAnswerNumberView() {
        binding.apply {
            when (answerNumber) {
                0 -> {
                    llAnswerA.gone()
                    llAnswerB.gone()
                    llAnswerC.gone()
                    llAnswerD.gone()
                    llAnswerE.gone()
                    rbAnswerA.gone()
                    rbAnswerB.gone()
                    rbAnswerC.gone()
                    rbAnswerD.gone()
                    rbAnswerE.gone()
                    selectImgA.gone()
                    selectImgB.gone()
                    selectImgC.gone()
                    selectImgD.gone()
                    selectImgE.gone()
                    llAddAnswer.visible()
                }

                1 -> {
                    llAnswerA.visible()
                    rbAnswerA.visible()
                    selectImgA.visible()
                    selectImgB.gone()
                    selectImgC.gone()
                    selectImgD.gone()
                    selectImgE.gone()
                    llAnswerB.gone()
                    llAnswerC.gone()
                    llAnswerD.gone()
                    llAnswerE.gone()
                    rbAnswerB.gone()
                    rbAnswerC.gone()
                    rbAnswerD.gone()
                    rbAnswerE.gone()
                    llAddAnswer.visible()
                }

                2 -> {
                    llAnswerA.visible()
                    rbAnswerA.visible()
                    llAnswerB.visible()
                    rbAnswerB.visible()
                    selectImgA.visible()
                    selectImgB.visible()
                    selectImgC.gone()
                    selectImgD.gone()
                    selectImgE.gone()
                    llAnswerC.gone()
                    llAnswerD.gone()
                    llAnswerE.gone()
                    rbAnswerC.gone()
                    rbAnswerD.gone()
                    rbAnswerE.gone()
                    llAddAnswer.visible()
                }

                3 -> {

                    llAnswerA.visible()
                    rbAnswerA.visible()
                    llAnswerB.visible()
                    rbAnswerB.visible()
                    llAnswerC.visible()
                    rbAnswerC.visible()
                    selectImgA.visible()
                    selectImgB.visible()
                    selectImgC.visible()
                    selectImgD.gone()
                    selectImgE.gone()
                    llAnswerD.gone()
                    llAnswerE.gone()
                    rbAnswerD.gone()
                    rbAnswerE.gone()
                    llAddAnswer.visible()
                }

                4 -> {
                    llAnswerA.visible()
                    rbAnswerA.visible()
                    llAnswerB.visible()
                    rbAnswerB.visible()
                    llAnswerC.visible()
                    rbAnswerC.visible()
                    llAnswerD.visible()

                    selectImgA.visible()
                    selectImgB.visible()
                    selectImgC.visible()
                    selectImgD.visible()
                    selectImgE.gone()
                    llAnswerE.gone()
                    rbAnswerD.visible()
                    rbAnswerE.gone()
                    llAddAnswer.visible()
                }

                5 -> {
                    llAnswerA.visible()
                    rbAnswerA.visible()
                    llAnswerB.visible()
                    rbAnswerB.visible()
                    llAnswerC.visible()
                    rbAnswerC.visible()
                    llAnswerD.visible()
                    llAnswerE.visible()
                    rbAnswerD.visible()
                    rbAnswerE.visible()

                    selectImgA.visible()
                    selectImgB.visible()
                    selectImgC.visible()
                    selectImgD.visible()
                    selectImgE.visible()
                    llAddAnswer.gone()
                }
            }
        }
    }


    override fun onClick() {
        binding.apply {
            tvOptionsE.onClick {
                answerNumber--
                setAnswerNumberView()
            }

            tvOptionsD.onClick {
                answerNumber--
                setAnswerNumberView()
            }
            tvOptionsC.onClick {
                answerNumber--
                setAnswerNumberView()
            }
            tvOptionsB.onClick {
                answerNumber--
                setAnswerNumberView()
            }
            tvOptionsA.onClick {
                answerNumber--
                setAnswerNumberView()
            }

            btnNext.onClick {
                addQuestionData(0)

            }

            tvWeiDu.onClick {
                recyclerDialog(6, "选择关联维度", "取消", "确定", textView = tvWeiDu)
            }


            llAddAnswer.onClick {
                answerNumber++
                setAnswerNumberView()
            }
            rgHasRightAnswer.setOnCheckedChangeListener { _, checkedId ->
                when (checkedId) {
                    R.id.rbYes -> {
                        ifright = "1"
                        llRightAnsewr.visible()
                        tvChoiceRightAnswer.visible()
                    }

                    R.id.rbNo -> {
                        ifright = "2"
                        llRightAnsewr.gone()
                        tvChoiceRightAnswer.gone()
                    }
                }
            }

            rbAnswerA.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    when (type) {
                        "1" -> {
                            rbAnswerA.isChecked = true
                            rbAnswerB.isChecked = false
                            rbAnswerC.isChecked = false
                            rbAnswerD.isChecked = false
                            rbAnswerE.isChecked = false
                            rightbiaoshi = "A"
                        }

                        "2" -> {
                            rbAnswerA.isChecked = true
                        }
                    }
                }
            }

            rbAnswerB.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    when (type) {
                        "1" -> {
                            rbAnswerA.isChecked = false
                            rbAnswerB.isChecked = true
                            rbAnswerC.isChecked = false
                            rbAnswerD.isChecked = false
                            rbAnswerE.isChecked = false
                            rightbiaoshi = "B"
                        }

                        "2" -> {
                            rbAnswerB.isChecked = true
                        }
                    }
                }
            }

            rbAnswerC.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    when (type) {
                        "1" -> {
                            rbAnswerA.isChecked = false
                            rbAnswerB.isChecked = false
                            rbAnswerC.isChecked = true
                            rbAnswerD.isChecked = false
                            rbAnswerE.isChecked = false
                            rightbiaoshi = "C"
                        }

                        "2" -> {
                            rbAnswerC.isChecked = true
                        }
                    }
                }
            }


            rbAnswerD.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    when (type) {
                        "1" -> {
                            rbAnswerA.isChecked = false
                            rbAnswerB.isChecked = false
                            rbAnswerC.isChecked = false
                            rbAnswerD.isChecked = true
                            rbAnswerE.isChecked = false
                            rightbiaoshi = "D"
                        }

                        "2" -> {
                            rbAnswerD.isChecked = true
                        }
                    }
                }
            }


            rbAnswerE.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    when (type) {
                        "1" -> {
                            rbAnswerA.isChecked = false
                            rbAnswerB.isChecked = false
                            rbAnswerC.isChecked = false
                            rbAnswerD.isChecked = false
                            rbAnswerE.isChecked = true
                            rightbiaoshi = "E"
                        }

                        "2" -> {
                            rbAnswerE.isChecked = true
                        }
                    }
                }
            }




            btnPlus.onClick {
                questionScore++
                tvNum.text = questionScore.toString()
            }
            btnMinus.onClick {
                if (questionScore > 0) {
                    questionScore--
                    tvNum.text = questionScore.toString()
                }
            }

        }
    }

    /**
     *@Description 添加题目
     *@date 创建时间2024/2/24
     *
     */
    private fun addQuestionData(addType: Int) {
        binding.apply {
            questionScore = 0
            if (questionType == "1") {
                listBean.clear()
                when (type) {
                    "1" -> {
                        if (etTitle.text().isEmpty()) {
                            toast("请输入问题")
                            return
                        }


                        if (llAnswerA.isVisible) {
                            if (etAnswerA.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("A", etAnswerA.text(), "1", imgUrlA))
                            } else {
                                toast("请输入答案A")
                                return
                            }
                        } else {
                            toast("请添加选项")
                            return
                        }

                        if (llAnswerB.isVisible) {
                            if (etAnswerB.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("B", etAnswerB.text(), "2", imgUrlB))
                            } else {
                                toast("请输入答案B")
                                return
                            }
                        } else {
                            toast("请添加选项")
                            return
                        }


                        if (llAnswerC.isVisible) {
                            if (etAnswerC.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("C", etAnswerC.text(), "3", imgUrlC))
                            } else {
                                toast("请输入答案C")
                                return
                            }
                        }

                        if (llAnswerD.isVisible) {
                            if (etAnswerD.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("D", etAnswerD.text(), "4", imgUrlD))
                            } else {
                                toast("请输入答案D")
                                return
                            }
                        }
                        if (llAnswerE.isVisible) {
                            if (etAnswerE.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("E", etAnswerE.text(), "5", imgUrlE))
                            } else {
                                toast("请输入答案E")
                                return
                            }
                        }

                        if (tvNum.text.toString() == "0") {
                            toast("请设置分值")
                            return
                        }

                        if (tvWeiDu.text.toString() == "请选择维度") {
                            toast("请选择维度")
                            return
                        }

                        if (etAnalysis.text().isEmpty()) {
                            toast("请输入答案解析")
                            return
                        }

                        when (ifright) {
                            "1" -> if (rightbiaoshi.isEmpty()) {
                                toast("请选择正确答案")
                                return
                            }
                        }


                    }

                    "2" -> {
                        rightbiaoshi = ""
                        if (rbAnswerA.isChecked)
                            rightbiaoshi = "A"
                        if (rbAnswerB.isChecked) {
                            if (rightbiaoshi.isEmpty()) {
                                rightbiaoshi = "B"
                            } else {
                                rightbiaoshi += "," + "B"
                            }
                        }
                        if (rbAnswerC.isChecked) {
                            if (rightbiaoshi.isEmpty()) {
                                rightbiaoshi = "C"
                            } else {
                                rightbiaoshi += "," + "C"
                            }
                        }
                        if (rbAnswerD.isChecked) {
                            if (rightbiaoshi.isEmpty()) {
                                rightbiaoshi = "D"
                            } else {
                                rightbiaoshi += "," + "D"
                            }
                        }
                        if (rbAnswerE.isChecked) {
                            if (rightbiaoshi.isEmpty()) {
                                rightbiaoshi = "E"
                            } else {
                                rightbiaoshi += "," + "E"

                            }
                        }

                        if (etTitle.text().isEmpty()) {
                            toast("请输入问题")
                            return
                        }
                        if (etAnswerA.text().isNotEmpty()) {
                            listBean.add(NormalQuestionDto("A", etAnswerA.text(), "1"))
                        } else {
                            toast("请输入答案A")
                            return
                        }
                        if (etAnswerB.text().isNotEmpty()) {
                            listBean.add(NormalQuestionDto("B", etAnswerB.text(), "2"))
                        } else {
                            toast("请输入答案B")
                            return
                        }
                        if (etAnswerC.text().isNotEmpty()) {
                            listBean.add(NormalQuestionDto("C", etAnswerC.text(), "3"))
                        } else {
                            toast("请输入答案C")
                            return
                        }
                        if (llAnswerD.isVisible) {
                            if (etAnswerD.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("D", etAnswerD.text(), "4"))
                            } else {
                                toast("请输入答案D")
                                return
                            }
                        }
                        if (llAnswerE.isVisible) {
                            if (etAnswerE.text().isNotEmpty()) {
                                listBean.add(NormalQuestionDto("E", etAnswerE.text(), "5"))
                            } else {
                                toast("请输入答案E")
                                return
                            }
                        }

                        if (tvNum.text.toString() == "0") {
                            toast("请设置分值")
                            return
                        }

                        if (tvWeiDu.text.toString() == "请选择维度") {
                            toast("请选择维度")
                            return
                        }

                        if (etAnalysis.text().isEmpty()) {
                            toast("请输入答案解析")
                            return
                        }
                        when (ifright) {
                            "1" -> if (rightbiaoshi.isEmpty()) {
                                toast("请选择正确答案")
                                return
                            }
                        }

                    }

                    "3" -> {
                        if (tvNum.text.toString() == "0") {
                            toast("请设置分值")
                            return
                        }

                        if (tvWeiDu.text.toString() == "请选择维度") {
                            toast("请选择维度")
                            return
                        }
                        if (etTitle.text().isEmpty()) {
                            toast("请输入问题")
                            return
                        }

                        if (etAnalysis.text().isEmpty()) {
                            toast("请输入答案解析")
                            return
                        }
                    }
                }

                val bubbleDialog = BubbleDialog(this@SetQuestionAct)
                scopeDialog(bubbleDialog) {
                    val baseBean = addcgtimu(
                        smallid,
                        type,
                        tvNum.text.toString(),
                        weiduId,
                        etTitle.text(),
                        listBean,
                        ifright,
                        rightbiaoshi,
                        etAnalysis.text()
                    )
                    if (baseBean.result == "0") {
                        rgState.clearCheck()
                        tvNum.text = "0"
                        tvWeiDu.text = "请选择维度"
                        weiduId = ""
                        etTitle.setText("")
                        etAnswerA.setText("")
                        etAnswerB.setText("")
                        etAnswerC.setText("")
                        etAnswerD.setText("")
                        etAnswerE.setText("")
                        rgHasRightAnswer.clearCheck()
                        rbAnswerA.isChecked = false
                        rbAnswerB.isChecked = false
                        rbAnswerC.isChecked = false
                        rbAnswerD.isChecked = false
                        rbAnswerE.isChecked = false

                        answerNumber = 0
                        tvChoiceRightAnswer.gone()
                        llRightAnsewr.gone()
                        setAnswerNumberView()

                        etAnalysis.setText("")
                        listBean.clear()
                        ifright = "2"
                        rightbiaoshi = ""

                        if (addType == 1) {
                            saveSuccessDialog("添加完成")
                            finish()
                        }
                    }
                }
            }
        }
    }
}